﻿namespace MyTRCP.Common.Models
{
    public class DevInfoType3 : BitWalkableObj
    {
        public byte VendorId { get; set; }
        public ushort ProductId { get; set; }
        public byte HwVer { get; set; }
        public ushort SoftVer { get; set; }
        public bool SupportRadio { get; set; }
        public bool SupportMediumPower { get; set; }
        public bool FixedLocSpkVol { get; set; }
        public bool NotSupportSoftPowerCtrl { get; set; }
        public bool HaveNoSpeaker { get; set; }
        public bool HaveHmSpeaker { get; set; }
        public byte RegionCount { get; set; }
        public bool SupportNOAA { get; set; }
        public bool GMRS { get; set; }
        public bool SupportVFO { get; set; }
        public bool SupportDMR { get; set; }
        public byte ChannelCount { get; set; }
        public byte FreqRangeCount { get; set; }
        public DevInfoType3(byte[] data):base(data)
        {
        }
        public override string ToString()
        {
            return $"SupportRadio:{SupportRadio};SupportMediumPower:{SupportMediumPower};NotSupportSoftPowerCtrl:{NotSupportSoftPowerCtrl};fixed_loc_spk_vol:{FixedLocSpkVol};have_no_speaker:{HaveNoSpeaker};have_hm_speaker:{HaveHmSpeaker};channel_count:{ChannelCount};support_noaa:{SupportNOAA};gmrs:{GMRS};support_vfo:{SupportVFO};support_dmr:{SupportDMR};hw_ver:{HwVer};soft_ver:{SoftVer};ProductId:{ProductId};VendorId:{VendorId};RegionCount:{RegionCount};freqRangeCount:{FreqRangeCount}";
        }

        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            return bw.
                WriteBits(VendorId, 8)
                .WriteBits(ProductId, 16)
                .WriteBits(HwVer, 8)
                .WriteBits(SoftVer, 16)
                .WriteBit(SupportRadio)
                .WriteBit(SupportMediumPower)
                .WriteBit(FixedLocSpkVol)
                .WriteBit(NotSupportSoftPowerCtrl)
                .WriteBit(HaveNoSpeaker)
                .WriteBit(HaveHmSpeaker)
                .WriteBits(RegionCount, 6)
                .WriteBit(SupportNOAA)
                .WriteBit(GMRS)
                .WriteBit(SupportVFO)
                .WriteBit(SupportDMR)
                .WriteBits(ChannelCount, 8)
                .WriteBits(FreqRangeCount, 4);
        }

        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            if (bw.ByteLen < 10)
                throw new ArgumentException("字节数组长度过小（10）", nameof(bw));
            VendorId = (byte)bw.ReadBits(8);
            ProductId = (ushort)bw.ReadBits(16);
            HwVer = (byte)bw.ReadBits(8);
            SoftVer = (ushort)bw.ReadBits(16);
            SupportRadio = bw.ReadBit();
            SupportMediumPower = bw.ReadBit();
            FixedLocSpkVol = bw.ReadBit();
            NotSupportSoftPowerCtrl = bw.ReadBit();
            HaveNoSpeaker = bw.ReadBit();
            HaveHmSpeaker = bw.ReadBit();
            RegionCount = (byte)bw.ReadBits(6);
            SupportNOAA = bw.ReadBit();
            GMRS = bw.ReadBit();
            SupportVFO = bw.ReadBit();
            SupportDMR = bw.ReadBit();
            ChannelCount = (byte)bw.ReadBits(8);
            FreqRangeCount = (byte)bw.ReadBits(4);
            return bw;
        }
    }
}